<?php
namespace catchAdmin\externalGoods\model\search;
use catchAdmin\externalGoods\model\Category;
use catchAdmin\externalGoods\service\CategoryService;
use think\Exception;
use think\facade\Db;

trait TenantGoodsSearch
{
//    public function searchArbitraryFieldAttr($query, $value, $data){
//        return $query->where('g.brand_name|g.goods_name|goods_keywords','like',"%$value%");
//    }

    //是否上架
    public function searchIsSaleAttr($query, $value, $data){
        return $query->where('is_sale', $value);
    }

    //品牌名
    public function searchBrandNameAttr($query, $value, $data){
        //return $query->where('brand_name','like',"%$value%");
        return $query->whereLike('brand_name', $value);
    }

    public function searchArbitraryCodeAttr($query, $value, $data){
        return $query->where('goods_spu|goods_name','like',"%$value%");
    }

    public function searchGoodsSpuAttr($query, $value, $data){
        return $query->where('goods_spu',$value);
    }

    public function searchGoodsSpuBatchAttr($query, $value, $data){
        return $query->whereIn('goods_spu',$value);
    }

    //商品名
    public function searchGoodsNameAttr($query, $value, $data){
        return $query->whereLike('goods_name', $value);
    }

    //贸易类型
    public function searchTradeTypeNameAttr($query, $value, $data){
        return $query->where('trade_type_name', $value);
    }

    //贸易类型
    public function searchTradeTypeIdAttr($query, $value, $data){
        return $query->where('trade_type_id', $value);
    }

    //商品分类
    public function searchCategoryIdAttr($query, $value, $data)
    {
//        $categoryModel = new Category();
//        $h_id = $categoryModel->where('id',$value)->value('cate_id');
        $h_id = $value;


        //取出所有下级分类
        $c_id = explode(',', (new CategoryService())->get_all_child_cate($h_id));
        $c_id[] = $h_id;

        $c_id = array_values(array_filter($c_id));

        return $query->where('category_id','in', $c_id);
    }

    //商品分类2
    public function searchCateIdAttr($query, $value, $data)
    {
        //$categoryModel = new Category();
        //$h_id = $categoryModel->where('id',$value)->value('cate_id');
        $h_id = $value;

        //取出所有下级分类
        $c_id = explode(',', (new CategoryService())->get_all_child_cate($h_id));
        $c_id[] = $h_id;

        $c_id = array_values(array_filter($c_id));

        return $query->where('category_id','in', $c_id);
    }

    //选择供应商
    public function searchSupplierIdAttr($query, $value, $data){
        return $query->where('supplier_id', $value);
    }
}